﻿using System;
using System.Collections.Generic;
using System.Text;
using Musicman.Common.Entities;
using Musicman.Common;
using Musicman.DAL;
using System.Data.Common;

namespace Musicman.Business
{
    public class UserMassageManager
    {
        # region singleton definition
        static readonly UserMassageManager instance = new UserMassageManager();

        public static UserMassageManager Instance
        {
            get
            {
                return instance;
            }
        }
        static UserMassageManager()
        {
        }
        # endregion

        #region select 

        public List<UserMessage> LoadUserMessages()
        {
            return LoadUserMessages(10, DateTime.MinValue, true);
        }

        public List<UserMessage> LoadUserMessages(int top, DateTime messageDate, bool after)
        {
            List<UserMessage> res = null;
            try 
            {
                List<SelectParameter> parameters = new List<SelectParameter>();
                string lastMsgID = "";
                if (messageDate != DateTime.MinValue)
                {
                    lastMsgID = String.Format(" AND UserPost.Date {0} @ID", after ? "<" : ">");
                    parameters.Add(new SelectParameter("@ID", System.Data.DbType.DateTime, messageDate));
                }
                string where = String.Format(@" AND UserPost.ID in (SELECT TOP {0} UserPost.ID FROM UserPost WHERE UserPost.ParentID is null {1} {2})", 
                        top, 
                        lastMsgID,
                        after ? "ORDER BY UserPost.Date DESC" : "");
                res = UserMessageDAO.Instance.LoadUserMessages(where, " ORDER BY UserPost.Date DESC", parameters);
                // we are at the first page and should display the whole page
                if (res.Count != 0 && res.Count < top && ! after)
                {
                    res = LoadUserMessages(top, res[0].MessageDate, true);
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to load user messages", ex);
            }
            return res;
        }

        public UserMessage GetFullMessage(long messageID)
        {
            UserMessage res = null;
            try
            {
                List<SelectParameter> parameters = new List<SelectParameter>();
                string where = @" AND UserPost.ID = @ID "; 
                parameters.Add(new SelectParameter("@ID", System.Data.DbType.Int64, messageID));
                List<UserMessage> messages = UserMessageDAO.Instance.LoadUserMessages(where, "", parameters);
                if (messages.Count == 1)
                {
                    res = messages[0];
                }
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to load user message", ex);
            }
            return res;
        }

        public int GetNewMessagesCount(long userID)
        {
            int res = 0;
            try
            {
                res = UserMessageDAO.Instance.GetNewMessagesCount(userID);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to load new messages count", ex);
            }
            return res;
        }

        #endregion

        #region update
        public bool UpdateUserMessage(UserMessage msg)
        {
            bool res = false;
            try
            {
                res = UserMessageDAO.Instance.UpdateUserMessage(msg);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to insert user messages", ex);
            }
            return res;
        }
        #endregion 

        #region insert

        public bool InsertMessage(UserMessage message)
        {
            bool res = false;
            try
            {
                res = UserMessageDAO.Instance.InsertMessage(message);
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to insert user messages", ex);
            }
            return res;
        }

        #endregion

        #region delete
        public bool DeleteMessage(long messageID)
        {
            bool res = false;
            try
            {
                UserMessageDAO.Instance.DeleteMessage(messageID);
                res = true;
            }
            catch (Exception ex)
            {
                LogManager.LogException("Unable to insert user messages", ex);
            }
            return res;
        }

        #endregion

    }
}
